package com.zhangsun.system.controller;

import com.zhangsun.base.bean.ResponseData;
import com.zhangsun.common.utils.ResponseUtil;
import com.zhangsun.system.entity.RoleEntity;
import com.zhangsun.system.service.RoleService;
import com.zhangsun.system.vo.role.RoleEditVo;
import com.zhangsun.system.vo.role.RolePageVo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;

@RestController
@RequestMapping("/role")
public class RoleController {

    @Autowired
    private RoleService roleService;

    /**
     * 获取角色
     *
     * @param vo 条件
     * @return 角色
     */
    @GetMapping("/findAll")
    public ResponseData findAll(RolePageVo vo) {
        Object result = roleService.findAll(vo);
        return ResponseUtil.success(result);
    }

    /**
     * 添加角色
     *
     * @param vo 角色
     * @return 结果
     */
    @PostMapping("/create")
    @RequiresPermissions("system:role:edit")
    public ResponseData create(@Validated RoleEditVo vo) {
        roleService.create(vo);
        return ResponseUtil.success();
    }

    /**
     * 获取角色
     *
     * @param id 角色id
     * @return 角色
     */
    @GetMapping("/detail/{id}")
    @RequiresPermissions("system:role:view")
    public ResponseData findById(@NotBlank @PathVariable String id) {
        RoleEntity roleEntity = roleService.findById(id);
        return ResponseUtil.success(roleEntity);
    }

    /**
     * 更新角色
     *
     * @param id 角色id
     * @param vo 角色
     * @return 结果
     */
    @PutMapping("/update/{id}")
    @RequiresPermissions("system:role:edit")
    public ResponseData update(@NotBlank @PathVariable String id, @Validated RoleEditVo vo) {
        roleService.update(id, vo);
        return ResponseUtil.success();
    }

    /**
     * 删除角色
     *
     * @param id 角色id
     * @return 结果
     */
    @DeleteMapping("/delete/{id}")
    @RequiresPermissions("system:role:delete")
    public ResponseData delete(@NotBlank @PathVariable String id) {
        roleService.delete(id);
        return ResponseUtil.success();
    }
}
